﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CDSBDemandControl.Presenter;
using CDSBDemandControl.Model;
using CDSBDemandControl.Configuration;
using System.Web.UI.HtmlControls;

namespace CDSBDemandControl.View.Web.PaginasInternas
{
    public partial class Ranking : System.Web.UI.Page, IDiscriminatorDemand
    {
        private Presenter.DiscriminatorDemand Presenter;

        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Cache.SetExpires(DateTime.Now.Subtract(new TimeSpan(24, 0, 0)));
            Response.Cache.SetCacheability(HttpCacheability.NoCache);

            if (!this.IsPostBack || this.Session["Presenter"] == null)
            {
                this.Presenter = new DiscriminatorDemand(this);

                this.Session["Presenter"] = this.Presenter;

                if (!this.IsPostBack) this.ListarRanking();
            }
            else
            {
                this.Presenter = (Presenter.DiscriminatorDemand)this.Session["Presenter"];
                this.Presenter.ResetView(this);
            }
        }

        private void ListarRanking()
        {
            if (Request.QueryString["reportType"] == null)
                this.Presenter.MountRankingDelays(string.Empty);
            else
                this.Presenter.MountRankingDelays(Request.QueryString["reportType"].ToString());
        }

        public void ExibirRanking(IEnumerable<DeveloperModel> developers, IEnumerable<DeveloperModel> leaders, IEnumerable<DeveloperModel> focalpoints)
        {
            this.grdRankingDesenvolvedores.DataSource = (from dev in developers
                                                         where dev.DeveloperTeam != "timeScrum"
                                                         select dev).ToList<DeveloperModel>();
            this.grdRankingDesenvolvedores.DataBind();

            this.grdRankingScrumTeam.DataSource = (from dev in developers
                                                   where dev.DeveloperTeam == "timeScrum"
                                                   select dev).ToList<DeveloperModel>();
            this.grdRankingScrumTeam.DataBind();

            this.grdRankingLideres.DataSource = leaders;
            this.grdRankingLideres.DataBind();

            this.grdRankingPontoFocal.DataSource = (from FocalPoint in focalpoints where FocalPoint.Mission == Mission.FocalPoint select FocalPoint).ToList<DeveloperModel>();
            this.grdRankingPontoFocal.DataBind();

            this.divRanking.Visible = true;

            this.GerarTotalizadorRanking(developers, leaders, focalpoints);

            // Insere texto de observação
            DateTime dt = DateTime.Parse(ConfigManager.GetPeriodStartDate());

            if (Request.QueryString["reportType"] != null)
            {
                dt = new DateTime(dt.Year, dt.Month, 1);
                dt = dt.AddMonths(-6);
            }
            this.ExibirObservacao(string.Format("Tasks no período de {0} à {1}.", dt.ToString("yyyy-MM-dd hh:mm:ss").ToBrazilianShortDateForUSAShortDate(), DateTime.Now.Date.ToShortDateString()));            

            // Exibe a data da última atualização
            (this.Master.FindControl("LblUltimoUpdate") as Label).Text = "Última atualização em: " + this.Presenter.DemandListOutOfDateLastUpdate.ToShortDateString() + " as " + this.Presenter.DemandListOutOfDateLastUpdate.ToShortTimeString();

            (this.Master.FindControl("txtTimeToCompare") as HtmlGenericControl).InnerHtml =
                this.Presenter.DemandListOutOfDateLastUpdate.Year + "," +
                (this.Presenter.DemandListOutOfDateLastUpdate.Month - 1) + "," +
                this.Presenter.DemandListOutOfDateLastUpdate.Day + "," +
                this.Presenter.DemandListOutOfDateLastUpdate.Hour + "," +
                this.Presenter.DemandListOutOfDateLastUpdate.Minute + "," +
                this.Presenter.DemandListOutOfDateLastUpdate.Second;

        }

        private void ExibirObservacao(string obs)
        {
            this.LblObsOrdenacao.Text = obs;
            this.LblObsOrdenacao.Visible = this.LblObsOrdenacao.Text.Length > 0 ? true : false;
        }

        protected void Grid_DataBinding(object sender, EventArgs e)
        {
            ((GridView)sender).Visible = true;
        }

        private void GerarTotalizadorRanking(IEnumerable<DeveloperModel> developers, IEnumerable<DeveloperModel> leaders, IEnumerable<DeveloperModel> focalpoints)
        {
            int Entregas;
            int Atrasos;
            Decimal Totalizador;

            Entregas = (from Leader in leaders select Leader.TasksImplemented).Sum();
            Atrasos = (from Leader in leaders select Leader.TasksReleasedLate).Sum();
            Totalizador = (Entregas != 0) ? Decimal.Round((100 * Atrasos) / Entregas, 0) : 0;            

            Entregas = (from Focal in focalpoints select Focal.TasksImplemented).Sum();
            Atrasos = (from Focal in focalpoints select Focal.TasksReleasedLate).Sum();
            Totalizador = (Entregas != 0) ? Decimal.Round((100 * Atrasos) / Entregas, 0) : 0;            
            
            LblLiderPontoFocal.Text = "Daniel Sales Dos Santos";            

            LblTotalLiderPontoFocal.Text = string.Format("Entregas: {0}; <br/>Atrasos: {1}; <br/>Percentual: {2}.", Entregas, Atrasos, Totalizador);

            Entregas = (from Developer in developers select Developer.TasksImplemented).Sum();
            Atrasos = (from Developer in developers select Developer.TasksReleasedLate).Sum();
            Totalizador = (Entregas != 0) ? Decimal.Round((100 * Atrasos) / Entregas, 0) : 0;            
            LblTotalCoordenador.Text = string.Format("Entregas: {0}; <br/>Atrasos: {1}; <br/>Percentual: {2}.", Entregas, Atrasos, Totalizador);

            divTotalRegDev.Visible = true;            
            divTotalRegPto.Visible = true;
        }

        #region IDiscriminatorDemand Members

        void IDiscriminatorDemand.ExibirFila(IEnumerable<CDSBDemandControl.Model.DemandModel> demandModels)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirEsteira(IEnumerable<CDSBDemandControl.Model.DemandModel> demandModels)
        {
            throw new NotImplementedException();
        }        

        void IDiscriminatorDemand.ExibirAtrasos(IEnumerable<CDSBDemandControl.Model.DemandModel> PendingDemands, IEnumerable<CDSBDemandControl.Model.DemandModel> Concludeddemands)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirFaturamento(List<CDSBDemandControl.Model.BillingModel> billing, bool canSeeBilling)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirCodeReview(IEnumerable<CDSBDemandControl.Model.DemandModel> WorkItemsWithCodeReview, IEnumerable<CDSBDemandControl.Model.DemandModel> WorkItemsWithoutCodeReview)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirBugsdeProducao(IEnumerable<CDSBDemandControl.Model.DemandModel> demandModels)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirScenariosAndTasks(List<CDSBDemandControl.Model.ScenarioModel> scenarios)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirListaDisp(IEnumerable<CDSBDemandControl.Model.DemandModel> list, IEnumerable<CDSBDemandControl.Model.DeveloperModel> modelAusencias, List<DateTime> fer)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirBugsdeProducaoUltimoMes(List<CDSBDemandControl.Model.DemandModel> BugList)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirFactoryWaitingList(List<CDSBDemandControl.Model.BillingModel> Billing, bool AllowsShow)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirAlocacao(IEnumerable<CDSBDemandControl.Model.DemandModel> demandModels)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirFactoryProductionList(List<BillingModel> billing, bool canSeeBilling)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirSolicitacoesSites(List<CDSBDemandControl.Model.DemandModel> demandModels)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirManagementTeamTasks(List<CDSBDemandControl.Model.DemandModel> demandModels)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
